package com.common.dongqiuhui.apps.comm.tools;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

public class SettingUtil {
	/**
	 *  * 设置亮度  
	 */
	public static void setScreenBrightness(Activity activity, int brightness) {
		WindowManager.LayoutParams params = activity.getWindow().getAttributes();
		if (brightness > 255 || brightness < 0) {
			return;
		}
		if (brightness < 30) {
			return;
		}
		params.screenBrightness = brightness / 255f;
		activity.getWindow().setAttributes(params);
	}
	/**
	 * 获取屏幕的亮度  
	 */
	public static int getScreenBrightness(Context mContext) {
		int value = 0;
		ContentResolver cr = mContext.getContentResolver();
		try {
			value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {

		}
		return value;
	}
	/**
	 * 保存亮度设置状态
	 */
	public static void saveBrightness(ContentResolver resolver, int brightness) {
		Uri uri = Settings.System.getUriFor("screen_brightness");
		Settings.System.putInt(resolver, "screen_brightness", brightness);
		// resolver.registerContentObserver(uri, true, myContentObserver);
		resolver.notifyChange(uri, null);
	}

	/**
	 * 判断是否开启了自动亮度调节
	 */
	public static boolean isAutoBrightness(ContentResolver aContentResolver) {
		boolean automicBrightness = false;
		try {
			automicBrightness = Settings.System.getInt(aContentResolver, "screen_brightness_mode") == 1;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return automicBrightness;
	}
	
	/**
	 * 开启亮度自动调节
	 * 
	 */
	public static void startAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(), "screen_brightness_mode", 1);
	}

	/**
	 * 停止自动亮度调节
	 */
	public static void stopAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(), "screen_brightness_mode", 0);
	}
	
}
